/**
 * @Author : JiGuangJie
 * @Date : 2023/5/24
 * @Time : 下午6:04
 * @Version : 1.0.0
 * @description :
 */

const _countVector = (start: Array<number>, end: Array<number>): Array<number> => {
    return [end[0] - start[0], end[1] - start[1]];
};

const _projectVectorOnVector = (displacement_vector: any, base: any): any => {
    const k = (displacement_vector[0] * base[0] + displacement_vector[1] * base[1]) / (base[0] * base[0] + base[1] * base[1]);
    return [base[0] * k, base[1] * k];
};

const _movePoint = (point: any, displacementVector: any): any => {
    return [point[0] + displacementVector[0], point[1] + displacementVector[1]];
};

const Transform = {
    scale: (opts: any) => {
        const {geometry, feature, index} = opts;
        const downCoordinate = [opts.oldPoint[0], opts.oldPoint[1]];
        const dragCoordinate = [opts.newPoint[0], opts.newPoint[1]];
        const center = opts.center;
        let scx = ((dragCoordinate)[0] - (center)[0]) / (downCoordinate[0] - (center)[0]);
        let scy = ((dragCoordinate)[1] - (center)[1]) / (downCoordinate[1] - (center)[1]);
        let displacementVector = [dragCoordinate[0] - downCoordinate[0], (dragCoordinate)[1] - downCoordinate[1]];
        geometry.applyTransform((g1: any, g2: any, dim: any): any => {
            if (dim < 2) return g2;

            let pointArray = [[6], [0, 8], [2], [4]];
            let pointA = [g1[0], g1[1]];
            let pointB = [g1[2], g1[3]];
            let pointC = [g1[4], g1[5]];
            let pointD = [g1[6], g1[7]];
            let pointA1 = [g1[8], g1[9]];

            console.log('==========g1', g1);
            console.log('==========g2', g2);
            console.log('======pointA', pointA);
            console.log('======pointB', pointB);
            console.log('======pointC', pointC);
            console.log('======pointD', pointD);
            console.log('======pointA1', pointA1);
            console.log('======downCoordinate', downCoordinate);


            const base = (index % 2 === 0) ? _countVector(pointA, pointB) : _countVector(pointD, pointA);
            const projectedVector = _projectVectorOnVector(displacementVector, base);
            const nextIndex = index + 1 < pointArray.length ? index + 1 : 0;
            const coordsToChange = [...pointArray[index], ...pointArray[nextIndex]];

            /*for (let j: number = 0; j < g1.length; j += dim) {
                g2[j] = coordsToChange.includes(j) ? g1[j] + projectedVector[0] : g1[j];
                g2[j + 1] = coordsToChange.includes(j) ? g1[j + 1] + projectedVector[1] : g1[j + 1];
            }*/
            [g2[2], g2[3]] = dragCoordinate;
            // console.log(aa);


            let projectedLeft, projectedRight;
            /*switch (index) {
                case 0:
                    displacementVector = _countVector(pointD, dragCoordinate);
                    projectedLeft = _projectVectorOnVector(displacementVector, _countVector(pointC, pointD));
                    projectedRight = _projectVectorOnVector(displacementVector, _countVector(pointA, pointD));
                    [g2[0], g2[1]] = _movePoint(pointA, projectedLeft);
                    [g2[4], g2[5]] = _movePoint(pointC, projectedRight);
                    [g2[6], g2[7]] = _movePoint(pointD, displacementVector);
                    [g2[8], g2[9]] = _movePoint(pointA1, projectedLeft);
                    break;
                case 1:
                    displacementVector = _countVector(pointA, dragCoordinate);
                    projectedLeft = _projectVectorOnVector(displacementVector, _countVector(pointD, pointA));
                    projectedRight = _projectVectorOnVector(displacementVector, _countVector(pointB, pointA));
                    [g2[0], g2[1]] = _movePoint(pointA, displacementVector);
                    [g2[2], g2[3]] = _movePoint(pointB, projectedLeft);
                    [g2[6], g2[7]] = _movePoint(pointD, projectedRight);
                    [g2[8], g2[9]] = _movePoint(pointA1, displacementVector);
                    break;
                case 2:
                    displacementVector = _countVector(pointB, dragCoordinate);
                    projectedLeft = _projectVectorOnVector(displacementVector, _countVector(pointA, pointB));
                    projectedRight = _projectVectorOnVector(displacementVector, _countVector(pointC, pointB));
                    console.log('==============', projectedLeft, projectedRight, displacementVector);
                    [g2[0], g2[1]] = _movePoint(pointA, projectedRight);
                    [g2[2], g2[3]] = _movePoint(pointB, displacementVector);
                    [g2[4], g2[5]] = _movePoint(pointC, projectedLeft);
                    [g2[8], g2[9]] = _movePoint(pointA1, projectedRight);
                    break;
                case 3:
                    displacementVector = _countVector(pointC, dragCoordinate);
                    projectedLeft = _projectVectorOnVector(displacementVector, _countVector(pointB, pointC));
                    projectedRight = _projectVectorOnVector(displacementVector, _countVector(pointD, pointC));
                    [g2[2], g2[3]] = _movePoint(pointB, projectedRight);
                    [g2[4], g2[5]] = _movePoint(pointC, displacementVector);
                    [g2[6], g2[7]] = _movePoint(pointD, projectedLeft);
                    break;
            }*/
            return g2;
        });
        feature.setGeometry(geometry);
        console.log('======transform scale', feature.getGeometry());

    }
};
export default Transform;